/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.dao.IdGenerator;
import com.minierp.dao.OutTypeDao;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.OutType;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class OutTypeFormCtr extends SimpleFormController {
	private OutTypeDao outTypeDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		new OutType();
		OutType form;
		if (id != null && !id.equals("")) {
			form = (OutType) this.outTypeDao.get(OutType.class,
					Long.parseLong(id));
		} else {
			form = new OutType();
			form.setSequenceCode(this.outTypeDao.getMaxSequenceCode() + 1);
		}

		return form;
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String id = request.getParameter("id");
		OutType obj = (OutType) command;
		String alertMsg = "";
		if (id != null && !id.equals("")) {
			this.outTypeDao.update(obj);
			alertMsg = "修改成功！";
		} else {
			obj.setId(this.idGenerator.getId(OutType.class.getName()));
			this.outTypeDao.save(obj);
			alertMsg = "增加成功！";
		}

		String continueFlag = request.getParameter("continue_flag");
		ModelAndView mv;
		if (continueFlag != null && continueFlag.equals("1")) {
			mv = new ModelAndView("redirect:/base/OutTypeNew.ihtm");
		} else {
			mv = new ModelAndView(this.getSuccessView());
			mv.addObject("returnValue", String.valueOf(obj.getId()));
		}

		EncCommonHelper.setAlertMessage(request, alertMsg);
		return mv;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public OutTypeDao getOutTypeDao() {
		return this.outTypeDao;
	}

	public void setOutTypeDao(OutTypeDao outTypeDao) {
		this.outTypeDao = outTypeDao;
	}
}